package com.appcenter.application.service;

import com.appcenter.application.dto.RolePermissionDto;
import com.appcenter.application.input.Permission.GetPermissionsInput;
import com.appcenter.application.input.RolePermission.GetRolePermissionsInput;
import com.appcenter.application.input.RolePermission.PageLoadInput;
import com.appcenter.application.input.RolePermission.SaveRolePermissionsInput;
import com.appcenter.application.mapper.*;
import com.appcenter.application.model.Permissions;
import com.appcenter.application.model.RolePermissions;
import com.appcenter.application.model.Roles;
import com.appcenter.application.output.RolePermission.GetRolePermissionsOutput;
import com.appcenter.application.output.RolePermission.PageLoadOutput;
import com.appcenter.application.output.RolePermission.SaveRolePermissionsOutput;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class RolePermissionService {
    @Autowired
    RoleUsersMapper roleUsersMapper;
    @Autowired
    PermissionsMapper permissionsMapper;
    @Autowired
    RolePermissionsMapper rolePermissionsMapper;
    @Autowired
    AppsMapper appsMapper;
    @Autowired
    RolesMapper rolesMapper;

    public GetRolePermissionsOutput GetRolePermissions(GetRolePermissionsInput input) {
        GetRolePermissionsOutput output = new GetRolePermissionsOutput();
        if (input.getAppId() == null || input.getAppId().length() == 0) {
            PagedResult<RolePermissionDto> pagedResult = new PagedResult(new ArrayList<RolePermissionDto>(), 0);
            output.setItems(pagedResult);
            return output;
        }
        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "Id desc");
        List<RolePermissionDto> ls = rolePermissionsMapper.searchRolePermission(input);
        PageInfo<RolePermissionDto> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<RolePermissionDto>(page.getList(), (int) page.getTotal()));
        return output;
    }

    public SaveRolePermissionsOutput SaveRolePermissions(SaveRolePermissionsInput input) {
        SaveRolePermissionsOutput output = new SaveRolePermissionsOutput();

        //获取当前角色下的权限信息
        List<RolePermissions> oldRolePermissions = rolePermissionsMapper.getRolePermissionByRoleId(input.getRoleId());
        List<RolePermissionDto> rolePermissionSummaryDtos = new ArrayList<RolePermissionDto>();
        if (input.getItems() != null && input.getItems().size() > 0) {
            for (int i = 0; i < input.getItems().size(); i++) {
                if (input.getItems().get(i).getChecked() != null && input.getItems().get(i).getChecked()) {
                    rolePermissionSummaryDtos.add(input.getItems().get(i));
                }
            }
        }
        //删除角色权限信息
        for (int i = 0; i < oldRolePermissions.size(); i++) {
            rolePermissionsMapper.deleteByPrimaryKey(oldRolePermissions.get(i).getId());
        }
        //添加角色权限信息
        for (int i = 0; i < rolePermissionSummaryDtos.size(); i++) {
            RolePermissions curRolePermission = new RolePermissions();
            String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            curRolePermission.setId(uuid);
            curRolePermission.setPermissionId(rolePermissionSummaryDtos.get(i).getPermissionId());
            curRolePermission.setRoleId(input.getRoleId());
            rolePermissionsMapper.insertSelective(curRolePermission);
        }
        return output;
    }

    public PageLoadOutput PageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setApps(appsMapper.selectAll());
        output.setRoles(rolesMapper.selectAll());
        output.setItems(GetRolePermissions(input).getItems());
        return output;
    }


}
